//
//  UserInformation.m
//  NJUSTHelper
//
//  Created by 郑越辉 on 2017/9/12.
//  Copyright © 2017年 njust. All rights reserved.userName
//

#import "UserInformation.h"

@implementation UserInformation

- (void) encodeWithCoder: (NSCoder *)coder{
    
    [coder encodeObject:self.userId forKey:@"userId"];
    [coder encodeObject:self.userName forKey:@"userName"];
    [coder encodeObject:self.userCode forKey:@"userCode"];
    [coder encodeObject:self.password forKey:@"password"];
    [coder encodeObject:@(self.isLogin) forKey:@"isLogin"];
    [coder encodeObject:self.libraryPwd forKey:@"libraryPwd"];
}

- (id)initWithCoder: (NSCoder *) coder{
    _userId = [[coder decodeObjectForKey:@"userId"] copy];
    _userName = [[coder decodeObjectForKey:@"userName"] copy];
    _userCode = [[coder decodeObjectForKey:@"userCode"] copy];
    _password = [[coder decodeObjectForKey:@"password"] copy];
    _isLogin = [[[coder decodeObjectForKey:@"isLogin"] copy] boolValue];
    _libraryPwd = [[coder decodeObjectForKey:@"libraryPwd"] copy];
    return self;
}

+ (void)saveUserInfo{
    [NSKeyedArchiver archiveRootObject:AccountInfo toFile:[self getPath]];
}

+ (UserInformation *)shareUserInfo{
    static dispatch_once_t pred;
    static UserInformation *shared = nil;
    dispatch_once(&pred, ^{
        shared = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getPath]];
        if (shared == nil) {
            shared = [[UserInformation alloc] init];
        }
    });
    return shared;
}

+ (NSString*)getPath{
    
    NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString* fileName = @"NJUSTUserInfo.plist";
    NSString *filePath = [docuPath stringByAppendingPathComponent:fileName];
    return filePath;
}
@end
